package com.supermap.business.cloud.suggest;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.business.cloud.push.MyWebSocket;
import com.supermap.business.push.getui.AppPush;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.cloud.push.Notice;
import com.supermap.dubbo.api.cloud.push.PushService;
import com.supermap.dubbo.api.cloud.suggest.Suggest;
import com.supermap.dubbo.api.cloud.suggest.SuggestService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2018年11月26 下午 5:54
 * @email 1301584033@qq.com
 */
@Controller
@RequestMapping("/cloud/suggest")
public class SuggestController {

    @Reference(timeout = 100000)
    private SuggestService suggestService;

    @Reference(timeout = 100000)
    private PushService pushService;
    /**
     * 专家建议上传
     * */
    @PutMapping("/putSuggest")
    @ResponseBody
    public JSONUtil putSuggest(@RequestBody Suggest suggest){
        JSONUtil jsonUtil=  suggestService.putSuggest(suggest);
        String noticetype=pushService.getProductNoticeTypes("quanguoapp2019131xsadwqeq");
        if(jsonUtil.getCode()==200){
            Notice notice = new Notice();
            String dateTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            notice.setUploadtime(dateTime);
            notice.setNoticetype(2);
            notice.setMessageid(jsonUtil.getCountSum());
            notice.setNoticetitle(suggest.getTitle());
            notice.setSenduserid(String.valueOf(suggest.getUserid()));
            if(suggest.getCrop()!=0) {
                //作物推送
                List<Integer> users=suggestService.getFarmIdByAreaCodes(suggest.getAreacode(),suggest.getCrop()+"");
                if(users!=null&&users.size()>0){
                    for(Integer userId:users){
                        notice.setReceiveuserid(String.valueOf(userId));
                        pushService.putNotice(notice);
                        //app总数推送
                        MyWebSocket.sendInfo(pushService.getReceiveuserIdCount(String.valueOf(notice.getReceiveuserid()),noticetype).getJsonStr(),String.valueOf(notice.getReceiveuserid()));
                        List<String> strings=pushService.getUserCode(notice.getReceiveuserid()+"");
                        AppPush.UserCodePush(strings,"您收到一条专家产品",notice.getNoticetitle());
                    }
                }

            }else{
                //作物区域推送
                //作物推送
                List<Integer> users=suggestService.getUserIdByAreaCodes(suggest.getAreacode());
                if(users!=null&&users.size()>0) {
                    for (Integer userId : users) {
                        notice.setReceiveuserid(String.valueOf(userId));
                        pushService.putNotice(notice);
                        //app总数推送
                        MyWebSocket.sendInfo(pushService.getReceiveuserIdCount(String.valueOf(notice.getReceiveuserid()),noticetype).getJsonStr(),String.valueOf(notice.getReceiveuserid()));
                        List<String> strings=pushService.getUserCode(notice.getReceiveuserid()+"");
                        AppPush.UserCodePush(strings,"您收到一条专家产品",notice.getNoticetitle());
                    }
                }
            }
        }
        return  jsonUtil.setCountSum(0);
    }

    /**
     * 获取建议列表
     * */
    @GetMapping("/getPageSuggestList")
    @ResponseBody
    public JSONUtil  getPageSuggestList(String userId,String farmId,String expertUserId,String suggestId,Integer pageNum,Integer pageSize,String keyName,String expertID){
        return  suggestService.getPageSuggestList(userId,farmId,expertUserId,suggestId,pageNum,pageSize,keyName,expertID);
    }

    /**
     * 点赞
     * */
    @PutMapping("/putPraiseSuggestList")
    @ResponseBody
    public JSONUtil putPraiseSuggestList(String userId,String suggestId){
        return  suggestService.putPraiseSuggestList(userId,suggestId);
    }

    /**
     * 取消点赞
     * */
    @DeleteMapping("/delPraiseSuggestList")
    @ResponseBody
    public JSONUtil delPraiseSuggestList(String userId,String suggestId){
        return  suggestService.delPraiseSuggestList(userId,suggestId);
    }


    @GetMapping("/getSuggestTypeList")
    @ResponseBody
    public List<Map<String,Object>> getSuggestTypeList(){
        return  suggestService.getSuggestTypeList();
    }

    @GetMapping("/getinterUrlByUserId")
    @ResponseBody
    public List<Map<String,Object>> getinterUrlByUserId(String userId){
        return  suggestService.getinterUrlByUserId(userId);
    }

    /**
     * 获取建议类型为预警信息的标题
     * */
    @GetMapping("/getSuggestTileTop1")
    @ResponseBody
    public List<Map<String,Object>>  getSuggestTileTop1(String areaCode){
        return  suggestService.getSuggestTileTop1(areaCode);
    }
    /**
     * 获取建议类型列表
     * */
    @GetMapping("/getPageSuggest")
    @ResponseBody
    public JSONUtil getPageSuggest(String areaCode,Integer type,Integer pageNum,Integer pageSize){
        return  suggestService.getPageSuggest(areaCode,type,pageNum,pageSize);
    }
}
